<template>
	<avue-crud :option="option" :data="data" v-model="form" ref="crud" @selection-change="selectionChange" @search-change="searchChange"
	 @search-reset="searchReset" @on-load="onLoad">
	</avue-crud>
</template>

<script>
	import request from '@/router/axios';
	import {
		getCodes
	} from "@/api/system/dict";

	export default {
		props: {
			carCode: "",
			status: "",
			selectionList: []
		},
		data() {
			return {
				form: {},
				query: {},
				option: {
					selection: true,
					searchShow: false,
					searchMenuSpan: 6,
					border: true,
					refreshBtn: false,
					columnBtn: false,
					addBtn: false,
					editBtn: false,
					delBtn: false,
					menu: false,
					column: [{
						label: "编号",
						prop: "code",
						width: "150px",
						disabled: true,
						placeholder: "系统自动生成"
					}, {
						label: "产废单位",
						prop: "organization",
						type: "select",
						dicData: [],
						props: {
							label: "name",
							value: "code"
						}
					}, {
						label: "备注",
						prop: "remark"
					}]
				},
				data: []
			};
		},
		computed: {
			// ids() {
			//   let ids = [];
			//   this.selectionList.forEach(ele => {
			//     ids.push(ele.id);
			//   });
			//   return ids.join(",");
			// }
		},
		created() {
			getCodes('51b223eb-73de-428c-bea7-98d85918302d').then(res => {
				this.statusItem = res.data.data;
			});
			request({
				url: '2/api/WmsOrganization/getpages',
				method: 'get',
				params: {
					limit: 0
				}
			}).then(res => {
				this.findObject(this.option.column, 'organization').dicData = res.data.data;
			});
		},
		mounted() {},
		methods: {
			selectionChange(list) {
				this.selectionList = list;
				this.$emit('closeMoule', list);
			},
			searchReset() {
				this.query = {};
				this.onLoad();
			},
			searchChange(params, done) {
				this.query = params;
				this.onLoad();
				done();
			},
			onLoad() {
				var where = "status=0";
				if (this.carCode != undefined && this.carCode != "") {
					where += " or carCode='" + this.carCode + "'";
				}
				request({
					url: '2/api/WmsOrder/getpages',
					method: 'get',
					params: {
						key: this.query.wasteName == undefined ? '' : this.query.wasteName,
						type: this.query.organization == undefined ? '' : this.query.organization,
						where: where,
						limit: 0
					}
				}).then(res => {
					const data = res.data
					data.data.forEach(ele => {
						if (ele.category != null) {
							var dataStrArr = ele.category.split(",");
							ele.category = [];
							dataStrArr.forEach(function(data) {
								ele.category.push(+data);
							});
						}
					});
					this.data = data.data;
					this.$nextTick(() => {
						if (this.selectionList != undefined && this.selectionList != "") {
							var select = [];
							this.data.forEach(ele => {
								this.selectionList.forEach(e => {
									if (ele.code == e.code) {
										select.push(ele);
										return;
									}
								});
							});
							this.$refs.crud.toggleSelection(select);
						}
					})
				});
			}
		}
	};
</script>

<style>
	.avue-crud__menu {
		min-height: 0px;
	}
</style>
